﻿@model RMS.Models.Problem
@using RMS.Models

@{
    ViewBag.Title = "Details";
    RMS.Models.RMSDataClassesDataContext db = new RMS.Models.RMSDataClassesDataContext();
    RMS.Models.User user = db.Users.FirstOrDefault(x => x.UserName == HttpContext.Current.User.Identity.Name);
    int topicNumber = 1;
}

<h2>Details</h2>

<fieldset>
    <legend>Problem</legend>

    <div class="display-label">ProblemGroup</div>
    <div class="display-field">@Model.ProblemGroup.Name</div>

    <div class="display-label">Product</div>
    @if (Model.ProductId.HasValue)
    {
        <div class="display-field">@Model.Product.Name</div>
    }
    else
    {
        <div class="display-field">General Product</div>
    }

    <div class="display-label">Name</div>
    <div class="display-field">@Model.Name</div>

    <div class="display-label">Risk Type</div>
    <div class="display-field">@Model.RiskType.Name</div>

    <div class="display-label">Causation</div>
    <div class="display-field">@Model.Causation</div>
</fieldset>

@if(Request.IsAuthenticated&&user.CompanyId.HasValue)
{
    if(Model.CompaniesProblems.Any(x=>x.CompanyId == user.CompanyId.Value))
    {
        ProblemScore companyProblemScore = Model.ProblemScores.FirstOrDefault(x => x.UserId == user.Company.FouderUserId);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <fieldset>
    <legend>Company Score [Scored only by @user.Company.FounderUser.Name ]</legend>
    <br />
    @if (companyProblemScore != null)
    {
        var score = companyProblemScore;
        <div class="matrix">
            <table>
                <tr>
                    <th>RVN</th>
                    <th>Impact</th>
                    <th>Probability</th>
                    <th>Discrimination</th>
                    <th>Detection</th>
                    <th>Risk</th>
                </tr>
                <tr>
                    
                    <th>
                        @companyProblemScore.RVN.ToString("n2")
                    </th>
                    <td>
                        @companyProblemScore.ImpactScore
                    </td>
                    <td>
                        @companyProblemScore.ProbabilityScore
                    </td>
                    <td>
                        @companyProblemScore.DiscriminationScore
                    </td>
                    <td>
                        @companyProblemScore.DetectionScore.ToString("n2")
                    </td>
                    <td>    
                        @companyProblemScore.RiskScore.ToString("n2")
                    </td>
                </tr>
            </table>
        </div>
        <br />
        <div class="matrix">
        <fieldset>
            <legend>Risk Matrix</legend>
            <table class="matrix">
                
                <tr>
                    <th rowspan="5">
                        Impact    
                    </th>
                    <th>
                        Very High
                    </th>
                    <td style="background-color: #FCAD25;">
                        @if ((score.ImpactScore == 5) && (score.ProbabilityScore == 1))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #FC3232;"></td>
                    <td style="background-color: #FC3232;">
                        @if ((score.ImpactScore == 5) && (score.ProbabilityScore == 3))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #FC3232;"></td>
                    <td style="background-color: #FC3232;">
                        @if ((score.ImpactScore == 5) && (score.ProbabilityScore == 5))
                        {
                            @Model.Name
                        }
                    </td>
                </tr>

                <tr>
                    <th>
                        High
                    </th>
                    <td style="background-color: #FCAD25;">
                        @if ((score.ImpactScore == 4) && (score.ProbabilityScore == 1))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #FCAD25;"></td>
                    <td style="background-color: #FCAD25;">
                        @if ((score.ImpactScore == 4) && (score.ProbabilityScore == 3))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #FC3232;"></td>
                    <td style="background-color: #FC3232;">
                        @if ((score.ImpactScore == 4) && (score.ProbabilityScore == 5))
                        {
                            @Model.Name
                        }
                    </td>

                </tr>
            
                <tr>
                    <th>
                        Medium
                    </th>
                    <td style="background-color: #E8E12E;">
                        @if ((score.ImpactScore == 3) && (score.ProbabilityScore == 1))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #E8E12E;"></td>
                    <td style="background-color: #FCAD25;">
                        @if ((score.ImpactScore == 3) && (score.ProbabilityScore == 3))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #FCAD25;"></td>
                    <td style="background-color: #FC3232;">
                        @if ((score.ImpactScore == 3) && (score.ProbabilityScore == 5))
                        {
                            @Model.Name
                        }
                    </td>
                </tr>

                <tr>
                <th>
                    Low
                </th>
                    <td style="background-color: #A7FDA1;">
                        @if ((score.ImpactScore == 2) && (score.ProbabilityScore == 1))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #E8E12E;"></td>
                    <td style="background-color: #E8E12E;">
                        @if ((score.ImpactScore == 2) && (score.ProbabilityScore == 3))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #FCAD25;"></td>
                    <td style="background-color: #FCAD25;">
                        @if ((score.ImpactScore == 2) && (score.ProbabilityScore == 5))
                        {
                            @Model.Name
                        }
                    </td>
                </tr>

                <tr>
                    <th>
                        Very Low
                    </th>
                    <td style="background-color: #A7FDA1;">
                        @if ((score.ImpactScore == 1) && (score.ProbabilityScore == 1))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #E8E12E;"></td>
                    <td style="background-color: #E8E12E;">
                    @if ((score.ImpactScore == 1) && (score.ProbabilityScore == 3))
                        {
                            @Model.Name
                        }
                    </td>
                    <td style="background-color: #E8E12E;"></td>
                    <td style="background-color: #FCAD25;">
                        @if ((score.ImpactScore == 1) && (score.ProbabilityScore == 5))
                        {
                            @Model.Name
                        }
                    </td>
                </tr>

                <tr>
                    <th rowspan="2" colspan="2"></th>
                    <th>Very Low</th>
                    <th>Low</th>
                    <th>Medium</th>
                    <th>High</th>
                    <th>Very High</th>

                </tr>
            
                <tr>
                    <th colspan="2"></th>
                    <th colspan="5">Probability</th>
                </tr>
            </table>

        </fieldset>
        </div>

        
    }
    else if (user.Company.FouderUserId == user.Id)
    {
        <text>You're <b>[Founder]</b> of this company. You're scoring willl appear as both company's score and shared score. (if problem is shared)</text>
    }
    else
    {
        <text>Please ask <b>@user.Company.FounderUser.Name</b> at email: <b>@Membership.GetUser(user.Company.FounderUser.UserName).Email</b> to add problem's score for your company.   </text>
    }
    <br /><br />
    <fieldset>
        <legend>Occurence</legend>
        <div class="display-label">Occur In Company @Model.Occurrences.Count(x=>x.User.CompanyId.Value == user.CompanyId.Value) Time(s)</div>
        <div class="display-label">Overall Occur @Model.Occurrences.Count() Time(s)</div>
        <div class="display-label">Average Cost: @try{ @Model.Occurrences.Average(x=>x.Cost).ToString("n2")  } catch { <text>0.00</text> } Baht</div>
        <div class="display-label">Overall Cost: @try{ @Model.Occurrences.Sum(x=>x.Cost).ToString() } catch { <text>0.00</text> } Baht</div>
        
        
        @using (Html.BeginForm("AddOccurence", "Problem", FormMethod.Post))
        {
            @Html.Hidden("problemId", Model.Id)
            <br />
            <fieldset>
                <legend>New Occurence</legend>
                    <div class="display-label">Add By</div>
                    <div class="display-field">@user.Name [@user.Company.Name]</div>
                    @Html.Hidden("userId",user.Id)

                    <div class="editor-label">
                        @Html.Label("Occure Date")
                    </div>
                    <div class="editor-field">
                        @Html.TextBox("occuredDate", DateTime.Now.ToShortDateString())
                    </div>

                    <div class="editor-label">
                        Cost (Average: @try {@Model.Occurrences.Average(x=>x.Cost).ToString("n2") } catch { <text>0.00</text> } Baht)
                    </div>
                    <div class="editor-field">
                        @try
                        {
                            @Html.TextBox("cost", Model.Occurrences.Average(x=>x.Cost))
                        }
                        catch
                        {
                            @Html.TextBox("cost", "0.00")        
                        }
                    </div>

                    <p>
                        <input type="submit" value="submit" />
                    </p>

            </fieldset>
        }
    </fieldset>
    <br />
    <fieldset>
    <legend>Best Practice</legend>
    <br />
    <table>
        <tbody>
            <tr><th class="top" colspan="5">Shared Best Practice</th></tr>
            <tr>
                <th>Name</th>
                <th>Average Score</th>
                <th>Created By</th>
                <th>Approved By</th>
                <th>Company's Best Practice</th>
            </tr>
            @foreach (var item in Model.BestPractices.Where(x => x.IsShared && x.ApprovalUserId.HasValue && !x.IsDeclined))
            {
                <tr>
                    <td>@item.Name</td>
                    <td>@item.BestPracticeScores.Average(x=>x.Score).ToString()</td>
                    <td>@item.CreateUser.Name [@item.CreateUser.Company.Name]</td>
                    <td>@item.ApprovalUser.Name </td>
                    
                    <td>
                        @if (item.CompaniesBestPractices.Any(x => x.CompanyId == user.Company.Id))
                        {
                            <text>Yes</text>
                        }
                        else
                        {
                            @Html.ActionLink("Add to Company's Best Practice Pool","AddToCompany","BestPractice",
                                new { id = item.Id, companyId = user.CompanyId.Value }, null)
                        }
                    
                        
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <br />
    <table>
        <tbody>
            <tr><th class="top" colspan="6">@user.Company.Name's Best Practice</th></tr>
            <tr>
                <th>Name</th><th>Company Score</th><th>Average Score</th><th>Status</th><th>*Scoring</th><th>Your Score</th>
            </tr>
            @foreach (var item in Model.BestPractices.Where(x=>x.CompaniesBestPractices.Any(y=>y.CompanyId == user.CompanyId.Value)))
            {
                <tr>
                    <td>@item.Name</td>
                    <td>
                        @if (item.BestPracticeScores.Any(x => x.User.Id == user.Company.FouderUserId))
                        {
                            @item.BestPracticeScores.FirstOrDefault(x => x.User.Id == user.Company.FouderUserId).Score.ToString()
                        }
                        else
                        {
                            <text>-</text>
                        }
                    </td>
                    <td>
                        @item.BestPracticeScores.Average(x=>x.Score).ToString()
                    </td>
                    <td>
                        @if (item.IsShared&&item.ApprovalUserId.HasValue&&!item.IsDeclined)
                        {
                            <text>Shared</text>
                        }else if(item.IsDeclined)
                        {
                            <text>Declined [ @item.Reason ]</text>
                        }
                        else if (item.IsShared && !item.ApprovalUserId.HasValue)
                        {
                            <text>Waiting for Approve</text>
                        } 
                        else
                        {
                            @Html.ActionLink("Send For Approve", "SendForApprove", "BestPractice", new
                               {
                                   id = item.Id,
                                   userId = user.Id
                               },null)
                        }
                    </td>
                    <td>
                        @using (Html.BeginForm("Scoring", "BestPractice", FormMethod.Get))
                        {
                                <input name="id" type="hidden" value="@item.Id" />
                                @Html.Hidden("userId", user.Id)
                                <div class="display-field">
                                    @Html.DropDownList("score", new SelectList(Enumerable.Range(1, 5).Select(x => x)), new { @class = "small" })
                                    <input type="submit" value="submit" class="small" />
                                </div>
                        }
                    </td>
                    <td>
                        @if (item.BestPracticeScores.Any(x => x.UserId == user.Id))
                        {
                            @Html.Encode(item.BestPracticeScores.FirstOrDefault(x => x.UserId == user.Id).Score.ToString())
                        }
                        else
                        {
                            <text>Not Yet</text>
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <p>
        * Only founder can have score for company
    </p>
        @using (Html.BeginForm("Create", "BestPractice",FormMethod.Post))
        {
            @Html.Hidden("ProblemId", Model.Id)
            @Html.Hidden("IsShared", false)
            @Html.Hidden("userId",user.Id)
            @Html.Hidden("CreateUserId", user.Id)
            <br />
            <fieldset>
                <legend>New Best Practice</legend>
                    <div class="display-label">Add By</div>
                    <div class="display-field">@user.Name [@user.Company.Name]</div>
                    @Html.Hidden("CreatedUserId", user.Id)

                    <div class="display-label">Name</div>
                    <div class="display-field">@Html.TextBox("Name","")</div>

                    <div class="display-label">Score</div>
                    <div class="display-field">@Html.DropDownList("score",new SelectList(Enumerable.Range(1,5).Select(x=>x)))</div>

                    <div class="display-label">Best Practice Group</div>
                    <div class="display-field">@Html.DropDownList("BestPracticeGroupId",new SelectList((IEnumerable<BestPracticeGroup>)ViewData["bestPracticeGroups"],"Id","Name"))</div>

                    <p>
                        <input type="submit" value="submit" />
                    </p>
            </fieldset>
        }
        </fieldset>
    </fieldset>
    }
    else
    {
                    <p>
    @Html.ActionLink("Add this problem to [" + user.Company.Name + "]'s problem pool", "AddProblemToCompany",
    new { companyId = user.CompanyId.Value, problemId = Model.Id })
    </p>
}
}

<fieldset>
    <legend>Shared Score</legend>
    <br />
    @if (Model.ProblemScores.Count() > 0)
    {
        <div class="matrix">
            <table>
                <tr>
                    <th>RVN</th>
                    <th>Impact</th>
                    <th>Probability</th>
                    <th>Discrimination</th>
                    <th>Risk</th>
                    <th>User</th>
                </tr>
                @foreach(var score in Model.ProblemScores)
                {
                    <tr>
                        <td>
                            @score.RVN
                        </td>
                        <td>
                            @score.ImpactScore
                        </td>
                        <td>
                            @score.ProbabilityScore
                        </td>
                        <td>
                            @score.DiscriminationScore
                        </td>
                        <td>
                            @score.RiskScore.ToString("n2")
                        </td>
                        <td>
                            @score.User.Name
                        </td>
                    </tr>
                }
                <tr>
                    <th colspan="5">Average Risk Score: @Model.ProblemScores.Average(x=>x.RiskScore).ToString("n2")</th>
                </tr>
            </table>
        </div>
    }
    else
    {
        <text>No score for this problem.</text>
    }
    <br />
     <fieldset>
        <legend>Occurence</legend>
        <div class="display-label">Occure (In all companies)</div>
        <div class="display-field">@Model.Occurrences.Count() Time(s)</div>
    </fieldset>
        
</fieldset>

@if(Request.IsAuthenticated)
{
    if(Model.ProblemScores.Any(x=>x.UserId == user.Id))
      {
          ProblemScore score = Model.ProblemScores.FirstOrDefault(x => x.UserId == user.Id);
          <br />
           using (Html.BeginForm("EditProblemScore","Problem",FormMethod.Post))
           {
                <fieldset>
            <legend>Change Score</legend>
            @Html.Hidden("problemScoreId", score.Id)
            @Html.Hidden("ProblemId",Model.Id)
            @Html.Hidden("UserId",user.Id)
            <div class="display-label">Impact Score</div>
            <div class="display-field">@Html.DropDownList("ImpactScore", Enumerable.Range(1, 5).Select(x => new SelectListItem
                                  {
                                      Text = x.ToString(),
                                      Value = x.ToString()
                                  }))
            </div>

            <div class="display-label">Probability Score</div>
            <div class="display-field">
                <select name="ProbabilityScore">
                    <option value="1">1</option>
                    <option value="3">3</option>
                    <option value="5">5</option>
                </select>
            </div>
    
            <div class="display-label">Discrimination Score</div>
            <div class="display-field">
                <select name="DiscriminationScore">
                    <option value="1">1</option>
                    <option value="3">3</option>
                    <option value="5">5</option>
                </select>
            </div>
            <div class="display-label">Detection Score</div>
            <div class="display-field">@Html.DropDownList("DetectionScore", Enumerable.Range(1, 5).Select(x => new SelectListItem
                                  {
                                      Text = x.ToString(),
                                      Value = x.ToString()
                                  }))
            </div>
            <p>
            <input type="submit" value="submit" />
            </p>
        </fieldset>
           }
      }
      else
      {
          using (Html.BeginForm("AddScore", "Problem", FormMethod.Post))
          {
                <fieldset>
            <legend>Scoring</legend>
            @Html.Hidden("problemId", Model.Id)
            <div class="display-label">Impact Score</div>
            <div class="display-field">@Html.DropDownList("impact", Enumerable.Range(1, 5).Select(x => new SelectListItem
                                  {
                                      Text = x.ToString(),
                                      Value = x.ToString()
                                  }))
            </div>

            <div class="display-label">Probability Score</div>
            <div class="display-field">
                <select name="probability">
                    <option value="1">1</option>
                    <option value="3">3</option>
                    <option value="5">5</option>
                </select>
            </div>
    
            <div class="display-label">Discrimination Score</div>
            <div class="display-field">
                <select name="discrimination">
                    <option value="1">1</option>
                    <option value="3">3</option>
                    <option value="5">5</option>
                </select>
            </div>
            <div class="display-label">Detection Score</div>
            <div class="display-field">@Html.DropDownList("detectionScore", Enumerable.Range(1, 5).Select(x => new SelectListItem
                                  {
                                      Text = x.ToString(),
                                      Value = x.ToString()
                                  }))
            </div>
            <p>
            <input type="submit" value="submit" />
            </p>
        </fieldset>
          }
      }
}

<fieldset>
    <legend>Comments</legend>
    @foreach (var topic in Model.ProblemTopics)
    {
        <br />  
        <table>
            <tr>
                <th>@(topicNumber++)</th>
                <th>Topic: @topic.Title</th>
                <th>User: @topic.User.UserName</th>
                <th>Date: @topic.Date</th>
                <th>
                    @if (Request.IsAuthenticated)
                    {
                        if (topic.ProblemTopicLikes.Any(x => x.UserId == user.Id))
                        {
                             @Html.ActionLink("Unlike", "ProblemTopicUnlike", "Topic", new { topicId = topic.Id, userId = user.Id },null)
                        }
                        else
                        {
                            @Html.ActionLink("Like", "ProblemTopicLike", "Topic", new { topicId = topic.Id, userId = user.Id },null)
                        }
                    }
                    else
                    {

                    }
                </th>
            </tr>
            <tr>
                <th></th>
                <th colspan="4">Like(s): 
                @foreach (var like in topic.ProblemTopicLikes)
                {
                    @Html.Encode(like.User.UserName)<text>&nbsp;</text>
                }
                </th>
            </tr>
            <tr>
                <th></th>
                <th colspan="4" style="background-color: #F2F2F2;">@topic.Details</th>
            </tr>
            @{int replyNumber = 1;
            }

            @foreach (var reply in topic.ProblemTopicReplies)
            {
                <tr>
                    <td>@(replyNumber++)</td>
                    <td>@reply.Details</td>
                    <td>User: @reply.User.UserName</td>
                    <td>Date: @reply.Date</td>
                    <td>
                    @if (Request.IsAuthenticated)
                    {
                        if (reply.ProblemTopicReplyLikes.Any(x => x.UserId == user.Id))
                        {
                             @Html.ActionLink("Unlike", "ProblemTopicReplyUnlike", "Topic", new { replyId = reply.Id, userId = user.Id }, null)
                        }
                        else
                        {
                            @Html.ActionLink("Like", "ProblemTopicReplyLike", "Topic", new { replyId = reply.Id, userId = user.Id }, null)
                        }
                    }
                    else
                    {

                    }
                    </td>
                </tr>
                <tr>
                    <td colspan="5">Like(s): 
                    @foreach (var like in reply.ProblemTopicReplyLikes)
                    {
                        @(Html.Encode(like.User.UserName))<text>&nbsp;</text> 
                    }
                    </td>
                </tr>
                
            }
            @if(Request.IsAuthenticated)
            {
                <tr>
                    @using (Html.BeginForm("AddProblemTopicReply", "Topic", FormMethod.Post))
                    {
                    <td colspan="5">
                    Reply: 
                    @Html.TextBox("details")
                    @Html.Hidden("userId", user.Id)
                    @Html.Hidden("topicId",topic.Id)
                    <input type="submit" value="submit" /> 
                    
                    </td>
                    }
                </tr>
            }
        </table>
        }
        @if (Request.IsAuthenticated)
        {
            <br />
            <table>
                @using (Html.BeginForm("AddProblemTopic", "Topic", FormMethod.Post))
                {
                    <tr>
                        <th>
                            Title: 
                        </th>
                        <th>
                            @Html.TextBox("title","")
                            @Html.Hidden("userId", user.Id)
                            @Html.Hidden("companyId", Model.Id)
                        </th>
                    </tr>
                    <tr>
                        <th>
                            Details:
                        </th>
                        <th>
                            @Html.TextArea("details")
                        </th>
                    </tr>
                    <tr>
                        <th></th>
                        <th>
                            <input type="submit" value="submit" />
                        </th>
                    </tr>
                }
            </table>
        }
</fieldset>


<p>
    @Html.ActionLink("Back to List", "Index")
</p>

